package com.dodoke.controller;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import com.dodoke.domain.WxPrizeRecord;
import com.dodoke.domain.vo.WxPrizeRecordVo;
import com.dodoke.service.IWxPrizeRecordService;
import com.dodoke.service.IWxUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dodoke.common.core.controller.BaseController;
import com.dodoke.common.core.domain.AjaxResult;

/**
 * 【中奖纪录】Controller
 *
 * @author dodoke
 * @date 2020-08-25
 */
@Api("中奖纪录")
@RestController
@RequestMapping("/record")
public class WxPrizeRecordController extends BaseController
{
    @Autowired
    private IWxPrizeRecordService wxPrizeRecordService;

    @Autowired
    private IWxUserService wxUserService;

    /**
     * 查询【中奖纪录】列表
     * @param openId 用户身份唯一标识
     */
    @ApiOperation(value = "查询中奖纪录列表")
    @ApiImplicitParam(name = "openId",value = "用户身份唯一标识",required = true,paramType = "path")
    @ApiResponse(code = 200,message = "获取用户中奖纪录成功")
    @GetMapping("/list/{openId}")
    public AjaxResult list(@PathVariable String openId)
    {
        List<WxPrizeRecordVo> wxPrizeRecordVos = wxPrizeRecordService.selectWxPrizeRecordByOpenId(openId);
        return AjaxResult.success(wxPrizeRecordVos);
    }

    @ApiOperation(value = "插入中奖纪录")
    @ApiImplicitParam(name = "openId",value = "用户身份唯一标识",required = true,paramType = "path")
    @ApiResponse(code=200,message = "插入中奖纪录成功")
    @RequestMapping("/insertPrizeRecord/{openId}")
    public AjaxResult insert(@PathVariable String openId, WxPrizeRecord wxPrizeRecord)
    {
        Long wxId = wxUserService.selectWxId(openId);
        wxPrizeRecord.setWxId(wxId);
        wxPrizeRecord.setDetailQrId(1L);
        //后端给数据库加过期时间
        Date date = new Date();
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE,1);
        date = calendar.getTime();
        wxPrizeRecord.setExpireTime(date);
        wxPrizeRecord.setType(0);
        int i = wxPrizeRecordService.insertWxPrizeRecordByOpenId(wxPrizeRecord);
        return toAjax(i);
    }




}
